package com.example.core.service;

import com.example.core.po.SysLicenseParam;
import de.schlichtherle.license.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.security.auth.x500.X500Principal;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.prefs.Preferences;


/****
 *
 * 许可操作类
 */
@Service
public class SysLicenseManagerServiceImpl implements SysLicenseManagerService {

    @Value("${sitc.path}")
    private String dirPath;


    private final static X500Principal DEFAULTHOLDERANDISSUER = new X500Principal("CN=Duke、OU=JavaSoft、O=Sun Microsystems、C=US");

    /****
     *
     * 通过自定义参数生成许可
     * @param sysLicenseParam
     * @return
     */
    @Override
    public void createLicense(SysLicenseParam sysLicenseParam) throws Exception {
        LicenseParam licenseParam = this.initLicenseParam(sysLicenseParam);
        LicenseManager licenseManager = new LicenseManager(licenseParam);
        LicenseContent licenseContent = this.initContext(sysLicenseParam);
        licenseManager.store(licenseContent, new File(dirPath + sysLicenseParam.getLicPath()));
    }


    /****
     *
     * 封装参数
     * @param sysLicenseParam
     * @return
     */
    @Override
    public LicenseParam initLicenseParam(SysLicenseParam sysLicenseParam) {
        Preferences preferences = Preferences.userNodeForPackage(this.getClass());
        CipherParam cipherParam = new DefaultCipherParam(sysLicenseParam.getStorePwd());
        KeyStoreParam keyStoreParam = new KeyStoreParam() {
            @Override
            public InputStream getStream() throws IOException {
                return new FileInputStream(dirPath + sysLicenseParam.getPrivateKey() + SysKeyStoreService.STORE);
            }

            @Override
            public String getAlias() {
                return sysLicenseParam.getPrivateAlias();
            }

            @Override
            public String getStorePwd() {
                return sysLicenseParam.getStorePwd();
            }

            @Override
            public String getKeyPwd() {
                return sysLicenseParam.getKeyPwd();
            }
        };
        return new DefaultLicenseParam(sysLicenseParam.getSubject(), preferences, keyStoreParam, cipherParam);
    }

    /****
     *
     * 初始化许可参数
     * @param sysLicenseParam
     * @return
     */
    @Override
    public LicenseContent initContext(SysLicenseParam sysLicenseParam) {
        LicenseContent content = new LicenseContent();
        content.setSubject(sysLicenseParam.getSubject());
        content.setHolder(DEFAULTHOLDERANDISSUER);
        content.setIssuer(DEFAULTHOLDERANDISSUER);
        content.setIssued(sysLicenseParam.getIssuedTime());
        content.setNotBefore(sysLicenseParam.getNotBefore());
        content.setNotAfter(sysLicenseParam.getNotAfter());
        content.setConsumerType(sysLicenseParam.getConsumerType());
        content.setConsumerAmount(sysLicenseParam.getConsumerAmount());
        content.setInfo(sysLicenseParam.getInfo());
        Map<String, String> map = new HashMap<>();
        if (!StringUtils.isEmpty(sysLicenseParam.getIp())) {
            map.put("ip", sysLicenseParam.getIp());
        }
        if (!StringUtils.isEmpty(sysLicenseParam.getMac())) {
            map.put("mac", sysLicenseParam.getMac());
        }
        content.setExtra(map);
        return content;
    }
}

